package pfc;

import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.Instance;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JComponent;

/**
 *
 * @author <Mejico, Rocío; Mignola, Natalia; Ordoñez, Agustina>
 */
public class GestorERS extends javax.swing.JPanel {

    private Project prj;

    public GestorERS(Project archivo) {
        prj = archivo;
    }

    /*carga las secciones del formulario de protege*/
    public JComponent ontoReq(String nombre) throws Excepciones {
        String nombreClase = "http://www.owl-ontologies.com/OntoReq.owl#" + nombre;
        JComponent panel = null;
        Cls clase = prj.getKnowledgeBase().getCls(nombreClase);
        int cantidad = prj.getKnowledgeBase().getInstances(clase).size();
        if (cantidad > 1) {
            throw new Excepciones("Proyecto NO válido.\nHay más de una instancia");
        }

        for (Iterator<Instance> it = prj.getKnowledgeBase().getInstances(clase).iterator(); it.hasNext();) {
            Instance iObj = (Instance) it.next();
            panel = (JComponent) prj.createRuntimeClsWidget(iObj);
        }

        if (panel == null) {
            throw new Excepciones("Proyecto NO válido.\nFaltan instancias");
        }

        return panel;
    }

    public JComponent ontoERS(String nombre) throws Excepciones {
        String nombreClase = "http://www.owl-ontologies.com/Ontology1324585894.owl#" + nombre;
        JComponent panel = null;
        Cls clase = prj.getKnowledgeBase().getCls(nombreClase);
        int cantidad = prj.getKnowledgeBase().getInstances(clase).size();
        if (cantidad > 1) {
            throw new Excepciones("Proyecto NO válido.\nHay más de una instancia");
        }

        for (Iterator<Instance> it = prj.getKnowledgeBase().getInstances(clase).iterator(); it.hasNext();) {
            Instance iObj = (Instance) it.next();
            panel = (JComponent) prj.createRuntimeClsWidget(iObj);
        }

        if (panel == null) {
            throw new Excepciones("Proyecto NO válido.\nFaltan instancias");
        }

        return panel;
    }

    /**
     * A partir de acá se llevan a cabo los métodos para obtener los datos de cada uno de los individuos correspondientes
     * para luego utilizar esos valores en el pdf a construir
     */
    private String introduccionValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Introduction")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#introductionDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String propositoValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Purpose")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#purposeDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String alcanceValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Scope")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/scopeDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String definicionesValores(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#DefinitionAcronymAndAbbreviation")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#descriptionsDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String referenciasValores(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Reference")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/referenceDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String visionValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Overview")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/overviewDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String descripcionValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#OverallDescription")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/overallDescriptionDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String perspectivaProductoValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#ProductPerspective")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#productPerspectiveDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String funcionesProductoValor(JenaOWLModel owlModel) {
        Instance i = null;

        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#ProductFunction")).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#ProductFuntionDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String instanciasUsuarioValor(JenaOWLModel owlModel, String nombre) {
        Instance i = null;
        String nombreClase = "http://www.owl-ontologies.com/Ontology1324585894.owl#" + nombre;
        Cls clase = owlModel.getCls(nombreClase);
        for (Iterator<Instance> it = owlModel.getInstances(clase).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#userCharacteristicDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String asunYdepValor(JenaOWLModel owlModel, String nombre) {
        Instance i = null;
        String nombreClase = "http://www.owl-ontologies.com/Ontology1324585894.owl#" + nombre;
        Cls clase = owlModel.getCls(nombreClase);
        for (Iterator<Instance> it = owlModel.getInstances(clase).iterator(); it.hasNext();) {
            i = it.next();
        }
        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#DependenciesDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String instanciasRestricValor(JenaOWLModel owlModel, String nombre) {
        Instance i = null;
        String nombreClase = "http://www.owl-ontologies.com/Ontology1324585894.owl#" + nombre;
        Cls clase = owlModel.getCls(nombreClase);
        for (Iterator<Instance> it = owlModel.getInstances(clase).iterator(); it.hasNext();) {
            i = it.next();
        }

        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#constraintDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String instanciasReqValor(JenaOWLModel owlModel, String nombre) {
        Instance i = null;
        String nombreClase = "http://www.owl-ontologies.com/OntoReq.owl#" + nombre;
        Cls clase = owlModel.getCls(nombreClase);

        for (Iterator<Instance> it = owlModel.getInstances(clase).iterator(); it.hasNext();) {
            i = it.next();
        }

        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/description");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String indiceValor(JenaOWLModel owlModel) {
        Instance i = null;
        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Index")).iterator(); it.hasNext();) {
            i = it.next();
        }

        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#indexDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    private String apendiceValor(JenaOWLModel owlModel) {
        Instance i = null;
        for (Iterator<Instance> it = owlModel.getInstances(owlModel.getCls("http://www.owl-ontologies.com/Ontology1324585894.owl#Appendix")).iterator(); it.hasNext();) {
            i = it.next();
        }

        OWLIndividual x = owlModel.getOWLIndividual(i.getName());
        OWLDatatypeProperty p = owlModel.getOWLDatatypeProperty("http://www.owl-ontologies.com/Ontology1324585894.owl#appendixDescription");
        if (!x.hasPropertyValue(p)) {
            return new String();
        } else {
            return (String) x.getPropertyValue(p);
        }
    }

    public Vector obtenerDatos() {
        Vector datos = new Vector();
        JenaOWLModel owlModel = (JenaOWLModel) prj.getKnowledgeBase();

        //pestaña 1
        datos.add(introduccionValor(owlModel));//0
        datos.add(propositoValor(owlModel));//1
        datos.add(alcanceValor(owlModel));//2
        datos.add(definicionesValores(owlModel));//3
        datos.add(referenciasValores(owlModel));//4
        datos.add(visionValor(owlModel));//5

        //pestaña 2
        datos.add(descripcionValor(owlModel));//6
        datos.add(perspectivaProductoValor(owlModel));//7
        datos.add(funcionesProductoValor(owlModel));//8

        String[] nombresU = {"EducationalLevelUser", "ExperienceUser", "TechnicalExpertiseUser"};
        for (int i = 0; i < nombresU.length; i++) {
            datos.add(instanciasUsuarioValor(owlModel, nombresU[i]));
        }

        String[] nombresR = {"AuditFunction", "ControlFunction", "CriticalityOfTheApplication",
            "HardwareLimitation", "HigherOrderLanguageRequirement",
            "InterfateToOtherApplication", "ParallelOperation", "RegulatoryPolicy",
            "ReliabilityRequirement", "SafetyAndSecurityConsideration", "SignalHandShakeProtocol"};
        for (int i = 0; i < nombresR.length; i++) {
            datos.add(instanciasRestricValor(owlModel, nombresR[i]));
        }

        datos.add(asunYdepValor(owlModel, "AssumptionAndDependency"));//23

        //pestaña 3
        String[] nombresRQ = {"AssociatedApplications", "PhysicalEnvironmetExpected", "StandardsFullfilment",
            "TechnologicalEnvironmentExpected", "CommunicationInterfaces", "HardwareInterfaces",
            "SoftwareInterfaces", "UserInterfaces", "FunctionalRequirements", "AppearanceRequirements",
            "LegacyRequirements", "PoliticalCulturalRequirements", "UseRequirements", "AccuracyRequirements",
            "CapacityRequirements", "CriticalSecurityRequirements", "SpeedRequirements", "Availability",
            "Portability", "Reliability", "Conficenciality", "Support", "ConfidentialInformation", "EDIExchange",
            "Adaptability", "Construction", "Durability", "EnvironmentalConditions", "PolicyAndRegulation",
            "SystemLifeCycleSustainment", "ComputingSupportStaff", "EmergenciesStaff", "GuestStaff", "ManagementControlServicesStaff",
            "ManagementStaff", "OperationStaff", "SecurityStaff", "SupportingStaff", "SystemReliability", "SystemSupport",
            "SystemPerformanceCharacteristics", "SystemSecurity"};

        for (int i = 0; i < nombresRQ.length; i++) {
            datos.add(this.instanciasReqValor(owlModel, nombresRQ[i]));
        }

        //pestaña 4
        datos.add(indiceValor(owlModel)); //66
        datos.add(apendiceValor(owlModel));//67

        return datos;
    }
}
//fin clase


